<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

class FeedbackView_list extends AppView
{
	
	var $template = 'list.html';
	
	
	function execute(&$obj, &$manager) {
		
		$this->addMsg('user_msg.ini');
		$this->addMsg('common_msg.ini', 'knowledgebase');
		
		
		$tpl = &new tplTemplatez($this->template_dir . $this->template);
		
		// filter sql
		$manager->setSqlParams($this->getFilterSql($manager));		
		

		// header generate
		$bp = &$this->pageByPage($manager->limit, $manager->getCountRecordsSql());
		$tpl->tplAssign('header', $this->commonHeaderList($bp->nav, $this->getFilter($manager), false));
		
		// sort generate
		$sort = &$this->getSort();		
		$manager->setSqlParamsOrder($sort->getSql());
		
		// get records
		$rows =& $this->stripVars($manager->getRecords($bp->limit, $bp->offset));
		
		// list records
		foreach($rows as $row) {
			
			$obj->set($row);
			
			$obj->set('question', substr($obj->get('question'), 0, 130) . '...');
			
			$attach_num = ($obj->get('attachment')) ? count(explode(';', $obj->get('attachment'))) : '-';
			$tpl->tplAssign('attachment_num', $attach_num);
			$tpl->tplAssign('username', $row['username']);
			$tpl->tplAssign('formated_date', $this->getFormatedDate($row['ts']));
			$tpl->tplAssign('category', $row['category_name']);

			$tpl->tplAssign($this->getViewListVars($obj->get('id'), $obj->get('answered'), $obj->get('placed')));
			
			$tpl->tplParse(array_merge($obj->get(), $this->msg), 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplAssign($sort->toHtml());
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function &getSort() {
		//$sort = new TwoWaySort();
		$sort = new OneWaySort($_GET);
		$sort->default_order = 1;
		$sort->setTitleMsg('asc',  $this->msg['sort_asc_msg']);
		$sort->setTitleMsg('desc', $this->msg['sort_desc_msg']);
		$sort->setSortItem('category_msg', 'cat', 'category_id', $this->msg['category_msg']);
		$sort->setSortItem('email_msg', 'email', 'email', $this->msg['email_msg']);
		$sort->setSortItem('username_msg', 'username', 'user_id', $this->msg['username_msg']);
		$sort->setSortItem('attachment_num_msg', 'attachment', 'attachment', array($this->msg['attachment_num_msg'], 6));
		
		$sort->setSortItem('answered_status_msg','status', 'answered', $this->msg['answered_status_msg']);
		$sort->setSortItem('date_posted_msg', 'date', 'date_posted', $this->msg['date_posted_msg'], 2);
		$sort->setSortItem('placed_status_msg', 'placed', 'placed', $this->msg['placed_status_msg']);
		
		
		//$sort->getSql();
		//$sort->toHtml()
		return $sort;
	}
	
	
	function getFilter($manager) {

		$select = new FormSelect();
		$select->css_class = 'colorInput';
		$select->setSelectWidth(250);
		$select->setOnChangeSubmit(true);
		$select->setSelectName('filter');
		$select->setRange(AppMsg::getMsgs('ranges_msg.ini', false, 'user_questions'));
		
		$arr = $_GET; unset($arr[$select->select_name]);		
		$selected = (isset($_GET['filter'])) ? $_GET['filter'] : 'all';
		
		$html = '<form action="" style="margin: 0px;">';
		$html .= http_build_hidden($arr);
		$html .= $select->select($selected);
		$html .= '</form>';
		
		return $html;
	}
	
	
	function getFilterSql($manager) {
		
		// filter
		$arr = array();
		@$value = addslashes($_GET['filter']);
		
		if($value && $value != 'all') {
			
			if($value == 'answered' || $value == 'not_answered') {
				$val = ($value == 'answered') ? 1 : 0;
				$f = 'e.answered';
				
			} else {
				$val = ($value == 'placed') ? 1 : 0;
				$f = 'e.placed';
			}
			
			$arr[] = "AND $f = $val";
		
		//default
		} //elseif($value != 'all') {
			//$arr[] = "AND e.answered = 0";
			
		//}
		
		return implode(" \n", $arr);
	}
	
	
	function getViewListVars($record_id = false, $answered = NULL, $placed = NULL) {
		
		// another title for uppdate button
		$msg = $this->msg['answer_msg'] . ' / ' . $this->msg['move_to_entries_msg'];
		$this->msg['update_msg'] = $msg;
		
		$this->inactive_style = 'color: #000000;';
		$row = parent::getViewListVars($record_id, $answered);
		
/*
		$active_img = ($active) ? 'active_1' : 'active_0';
		$row['answered_img'] = $this->getImgLink('', $active_img, '');
		
		$active_img = ($moved) ? 'active_1' : 'active_0';
		$row['placed_img'] = $this->getImgLink('', $active_img, '');	
*/	
		
		$row['email_link'] = $this->getActionLink('answer', $record_id);
		$row['email_img'] = $this->getImgLink($row['email_link'], 'email', $this->msg['answer_msg']);
		
		if(!$this->priv->isPriv('status')) {
			$answered = ($answered == 0) ? 'not' : 'not_checked';
			$placed = ($placed == 0) ? 'not' : 'not_checked';
		}
		
		$active_var = ($answered == 0) ? '1' : '0';
		$active_img = ($answered == 0) ? 'active_0' : 'active_1';
		$row['active_link'] = $this->getActionLink('answer_status', $record_id) . '&status=' . $active_var;
		$row['answered_img'] = $this->getImgLink($row['active_link'], $active_img, $this->msg['set_status_msg'], $this->msg['sure_status_msg']);
				
		$active_var = ($placed == 0) ? '1' : '0';
		$active_img = ($placed == 0) ? 'active_0' : 'active_1';
		$row['active_link'] = $this->getActionLink('place_status', $record_id) . '&status=' . $active_var;
		$row['placed_img'] = $this->getImgLink($row['active_link'], $active_img, $this->msg['set_status_msg'], $this->msg['sure_status_msg']);
				
		$more_param = array('question_id' => $record_id, 
		                    'referer' => WebUtil::serialize_url($this->controller->getCommonLink()));
		$link = $this->controller->getLink('knowledgebase', 'kb_entry', false, 'question', $more_param);
		$row['update_link'] = $link;
		$row['update_img'] = $this->getImgLink($row['update_link'], 'load', $this->msg['place_to_kb_msg']);
		
		return $row;
	}
}
?>